<template>
<div>
  <el-radio-group v-model="type" @input="handleInput">
    <el-radio-button label="1">食谱</el-radio-button>
    <el-radio-button label="5">视频</el-radio-button>
    <el-radio-button label="8">资讯</el-radio-button>
  </el-radio-group>
<!--从PersonalManagementView里面把表格代码赋值过来-->

  <el-table :data="contentArr">
    <el-table-column type="index" label="编号"></el-table-column>
    <el-table-column prop="title" label="商品标题" width="100px"></el-table-column>
    <el-table-column prop="brief" label="商品摘要" width="300px"></el-table-column>
    <el-table-column prop="categoryName" label="商品类型"></el-table-column>
    <el-table-column label="商品封面" width="100px">
      <template slot-scope="scope">
        <!--scope.row得到当前行对应的对象-->
        <img :src="BASE_URL+scope.row.imgUrl" width="80px">
      </template>
    </el-table-column>
    <el-table-column prop="viewCount" label="浏览量"></el-table-column>
    <el-table-column prop="commentCount" label="评论量"></el-table-column>
    <el-table-column prop="createTime" label="发布时间"></el-table-column>
    <!-- fixed="right" 固定到右侧-->
    <el-table-column label="操作" width="200px" fixed="right">
      <template slot-scope="scope">
        <el-button size="mini" type="success"
                   @click="handleEdit(scope.$index, scope.row)">修改</el-button>
        <el-button size="mini" type="danger"
                   @click="handleDelete(scope.$index, scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
</div>
</template>

<script>
export default {
  name: "AdminContentView",
  data() {
    return {
      type:"1",
      contentArr: [],
    }
  },
  methods: {
    handleEdit(i, c) {
      //跳转到修改页面
      this.$router.push('/personal/post?id='+c.id);
      //记录从后台前往的修改页面
      localStorage.editPageFrom='admin';
    },
    handleDelete(i,c){
      if (confirm("您确认删除此内容吗?")){
        this.axios.post(this.BASE_URL+'/v1/contents/'+c.id+"/delete")
        .then((response)=>{
          if (response.data.code==1){
            this.contentArr.splice(i,1);
            this.$message.success("删除成功!");
          }
        })
      }
    },
    handleInput(){
      this.loadContentByType(this.type);
    },
   loadContentByType(type){
     this.axios.get(this.BASE_URL+'/v1/contents/'+type+"/admin")
     .then((response)=>{
       if (response.data.code==1){
         this.contentArr = response.data.data;
       }
     })
   }
  },
  mounted() {
    //请求所有食谱数据
    this.loadContentByType(1);
  }
}
</script>

<style scoped>

</style>